package com.zm.exercise.day02;

import java.util.HashMap;

/**
 * @Author:Domen
 * @Date:2024/7/11 17:16
 */

public class XuTest {


    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }



    public static int lengthOfLongestSubstring(String s) {

        //滑动窗口利用hashmap进行解答
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int maxLen = 0;
        int left = 0;
        for(int i = 0; i < s.length();i++){
            //containsKey() 如果map里面已有这个值返回true 否则返回false
            if(map.containsKey(s.charAt(i))){
                left = Math.max(left, map.get(s.charAt(i))+1); //更新左指针的值
            }
            map.put(s.charAt(i),i); //记录此时值的下标
            maxLen = Math.max(maxLen, i - left + 1);    //更新结果
        }
        return maxLen;
    }
}
